package com.yf.esms.controller;

import com.yf.esms.common.ResultComponent;
import com.yf.esms.model.vo.IndexInfoVo;
import com.yf.esms.service.IndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author 99698
 */
@Api(tags = "es索引")
@Slf4j
@CrossOrigin
@RequestMapping("/index")
@RestController
public class IndexController {

    @Autowired
    private IndexService indexService;

    @GetMapping("/create")
    @ApiOperation(value = "创建索引")
    public ResultComponent<Boolean> createIndex(@RequestParam("indexName") String indexName) throws IOException {
        return new ResultComponent<>(true,"创建索引"+indexName+"成功！","200",indexService.createIndex(indexName));
    }

    @GetMapping("/selectOneByIndex")
    @ApiOperation(value = "查询某一个索引")
    public ResultComponent<IndexInfoVo> selectOneByIndex(@RequestParam("indexName") String indexName) throws IOException {
        try {
            return new ResultComponent<>(true,"查询索引"+indexName+"成功！","200",indexService.selectOneByIndex(indexName));
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return new ResultComponent<>(true,"查询索引"+indexName+"失败！该索引可能已被删除！","1001",new IndexInfoVo());
        }
    }

    @GetMapping("/deleteOneIndex")
    @ApiOperation(value = "删除一个索引")
    public ResultComponent<Boolean> deleteOneIndex(@RequestParam("indexName")String indexName) throws IOException {
        try {
            return new ResultComponent(true,"删除索引"+indexName+"成功！","200",indexService.deleteOneIndex(indexName));
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return new ResultComponent(true,"删除索引"+indexName+"失败！该索引可能已经被删除，请勿重复操作！","1002",false);
        }
    }


}
